pp108 : JMX Counters

JMX Counters

This topic describes the counters for managed components.

Counters expose performance data to systems managers and health analysis programs. Performance counters created for managed components are exposed as JMX attributes.

Types of counters

Value Counter

A Value Counter exposes some internal value. A thermometer would be a good metaphor. It has a value that can be retrieved at any given moment. The thermometer has no statistics; if you want to calculate an average temperature, you will have to sample the thermometer. The statistics are described here.

Property-based Value Counter

A property-based value counter is functionally identical to a value counter but it's based on a property getter method. It invokes the getter method of the property when queried for the value. The statistics are described here.

Event Occurrence Counter

An Event Occurrence Counter is used to register each occurrence of an event. To stay in weather metaphors, it could be used to keep track of the number of showers per day. The counter has a configurable moving average window in which it calculates the number of events per second. Besides that, it keeps track of the number of events since the counter reset. The statistics are described here.

Event Value Counter

An Event Value Counter is used to register the value of each occurrence of an event. You could for instance use this to keep track of the amount of rain per shower. Besides the statistics collected by an Event Occurrence Counter, it keeps track of the total value since the counter reset and it uses the moving average window to calculate the minimum, maximum, standard deviation and average event value (e.g. amount of rain). The statistics are described here.

Timer Event Value Counter

A special flavor of the Event Value Counter is the Timer Event Value Counter. This counter type has the event duration as counter value. The statistics are described here; the value of the counter is time duration in milliseconds.

Defining a counter

Counters can be defined on the managed component. The method createPerformanceCounter can be used to create different types of counters. Each counter type is covered in a separate sub-section. CounterFactory acts as a factory to create different kinds of performance counters.

Value Counter

A regular value counter requires that the counter's setCurrentValue is invoked whenever the counter's value changes. This will not be a very common counter type. The property based value counter is easier to use.
Example:

// Defining the counter
IValueCounter ctr = (IValueCounter) m_managedComponent.createPerformanceCounter(
"temperature",
Messages.CURRENT_TEMP_DESC,
CounterFactory.VALUE_COUNTER);
// Setting a value on it
ctr.setCurrentValue(32);

Property Based Value Counter

Each time the value of a property-based value counter is retrieved, it invokes the getter method of the related property.
Example (taken from com.eibus.util.threadpool.Dispatcher):

// Defining the counter
m_managedComponent.createPropertyBasedValueCounter(
"numIdleWorkers",
Messages.NO_IDLE_WORKERS_DESC
"numIdleWorkers",
this);

Event Occurrence Counter

To add an event to an Event Occurrence Counter, one must call addEvent on it.
Example (taken from com.eibus.directory.soap.Cache):

// Defining the counter
IEventOccurrenceCounter m_lookupCounter =
(IEventOccurrenceCounter)        m_managedComponent.createPerformanceCounter(
"lookups",
Messages.CACHE_LOOKUPS_DESC",
CounterFactory.EVENT_OCCURRENCE_COUNTER);
// Adding an event to the counter
m_lookupCounter.addEvent();

Event Value Counter

To add an event to an Event Value Counter, one must call addEvent on it.
Example (taken from com.eibus.web.tools.download.Download):

// Defining the counter
IEventValueCounter m_downloadSizeCounter = (IEventValueCounter)    managedComponent.createPerformanceCounter(
"downloadSize",
Messages.DOWNLOAD_SIZE_DESC,
CounterFactory.EVENT_VALUE_COUNTER);
// Adding an event to the counter
m_downloadSizeCounter.addEvent(fileSize);

Timer Event Value Counter

To add an event to a Timer Event Value Counter, you must do two calls on the counter. Before the actual event, start must be called. This method returns a long value that must be passed in the finish call after the event has occurred.
Example (taken from com.eibus.web.isapi.WebApplication):

// Defining the counter
ITimerEventValueCounter m_requestHandlingDurationCounter =
(ITimerEventValueCounter)
m_managedComponent.createPerformanceCounter(
"requestHandlingDuration",
Messages.REQUEST_HANDLING_FREQUENCY_DESC,
CounterFactory.TIMER_EVENT_VALUE_COUNTER);
// Adding an event to the counter
long startTime = m_requestHandlingDurationCounter.start();
service(ecb);
m_requestHandlingDurationCounter.finish(startTime);